package excelpptword2pdf.hsj.components.impl;

import com.aspose.words.Document;
import com.aspose.words.License;
import com.aspose.words.SaveFormat;
import excelpptword2pdf.hsj.components.AbstractPDFConverter;
import org.apache.log4j.Logger;
import org.springframework.core.io.ClassPathResource;

import java.io.*;

/**
 * 
  * 将word转换为PDF
  *
 * @author 石亚宁
 * @date 2020年7月13日 下午2:47:23
 * @version 1.0
 *
 */
public class Word2PDFConverter extends AbstractPDFConverter {


	private static Logger logger = Logger.getLogger(Word2PDFConverter.class);


	public Word2PDFConverter() {
		super();
	}

	public Word2PDFConverter(String fontDir) {
		super(fontDir);
	}

	@Override
	protected void initFileType() {
		fileTypes.add("txt");
		fileTypes.add("doc");
		fileTypes.add("docx");
		fileTypes.add("wps");
	}

	@Override
	protected boolean matchLicense() {
		boolean result = false;
//		InputStream is = PDF2WordConverter.class.getClassLoader().getResourceAsStream("com/sbr/converter/components/licenses/license.xml");
		try {
		InputStream is = new ClassPathResource("license.xml").getInputStream();
		License wordLicense = new License();
			wordLicense.setLicense(is);
			result = true;
		} catch (Exception e) {
			logger.warn("载入word授权文件失败");
		}
		return result;
	}

	@Override
	protected void convert2PDF(File sourceFile,File targetFile) {
		FileOutputStream os = null;
		try {
			os = new FileOutputStream(targetFile);
			Document doc = new Document(sourceFile.getAbsolutePath());
			doc.save(os, SaveFormat.PDF);
		} catch (FileNotFoundException e) {
			logger.error("输出到"+targetFile.getAbsolutePath()+"错误:"+e);
		} catch (Exception e) {
			logger.error("转换word出错:"+e);
		}finally {
			if(os!=null) {
				try {
					os.close();
				} catch (IOException e) {
					logger.error("关闭输出流出错:"+e);
				}
			}
		}
	}
	
	

}
